package com.seckill.act.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seckill.act.mapper.ActivityMapper;
import com.seckill.act.mapper.SeckillTimeMapper;
import com.seckill.act.pojo.Activity;
import com.seckill.act.pojo.SeckillTime;
import com.seckill.act.service.ActivityService;
import com.seckill.act.vo.ActivityVo;
import com.seckill.framework.mybatisplus.WrapperParse;
import com.seckill.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 作者
 * @since 2022-03-04
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private SeckillTimeMapper seckillTimeMapper;

    /**
     * Activity条件+分页查询
     * @param activity 查询条件
     * @return 分页结果
     */
    @Override
    public ActivityVo findPage(Activity activity){
        //活动查询
        List<Activity> activities = activityMapper.activityList(activity);

        //查询出所有时间
        //List<SeckillTime> seckillTimes = seckillTimeMapper.selectList(WrapperParse.simpleColumn("status",1));
        List<SeckillTime> seckillTimes = seckillTimeMapper.selectAllList();
        ActivityVo activityVo = new ActivityVo(activities,seckillTimes,seckillTimes.size());
        return activityVo;
    }

    /**
     * Activity分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Activity> findPage(int page, int size){
        //静态分页
        IPage<Activity> iPage = new Page<Activity>(page,size);
        //分页查询
        QueryWrapper<Activity> wrapper = new QueryWrapper<Activity>();
        wrapper.eq("is_del",1);
        return (Page<Activity>) activityMapper.selectPage(iPage,wrapper);
    }

    /**
     * Activity条件查询
     * @param activity
     * @return
     */
    @Override
    public List<Activity> findList(Activity activity){
        //构建查询条件
        QueryWrapper<Activity> wrapper = WrapperParse.simple(activity);
        //根据构建的条件查询数据
        List<Activity> activities = activityMapper.selectList(wrapper);

        for (Activity act : activities) {
            //查询出所有活动时间
            SeckillTime seckillTime = seckillTimeMapper.selectById(act.getTimeId());
            //查询出当前
            Map<String,Object> mp = new HashMap<String,Object>();
            mp.put("status",1);
            List<SeckillTime> seckillTimes = seckillTimeMapper.selectByMap(mp);
            act.setSeckillTime(seckillTime);
        }
        return activities;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        Activity activity = new Activity();
        activity.setIsDel(2);
        activity.setId(id);
        activityMapper.updateById(activity);
    }

    /**
     * 修改Activity
     * @param activity
     */
    @Override
    public void update(Activity activity) throws ParseException {
        //获取时段数据
        SeckillTime seckillTime = seckillTimeMapper.selectById(activity.getTimeId());
        activity.setSeckillTime(seckillTime);
        activity.setBegintime(TimeUtil.mergeDate(activity.getStartdate(),activity.getSeckillTime().getStarttime()));
        activity.setEndtime(TimeUtil.mergeDate(activity.getStartdate(),activity.getSeckillTime().getEndtime()));

        //如果结束时间<开始时间，则重新计算结束时间
        if(activity.getEndtime().getTime()<=activity.getBegintime().getTime()){
            activity.setEndtime(TimeUtil.mergeDate(TimeUtil.addDateHour(activity.getStartdate(),24),activity.getSeckillTime().getEndtime()));
        }
        activity.setTimeId(activity.getSeckillTime().getId());
        float times = TimeUtil.dif2hour(activity.getBegintime(), activity.getEndtime());
        activity.setTotalTime(times);
        //添加
        activityMapper.updateById(activity);
    }

    /**
     * 增加Activity
     * @param activity
     */
    @Override
    public void add(Activity activity) throws ParseException {
        //时间查询
        SeckillTime seckillTime = seckillTimeMapper.selectById(activity.getTimeId());
        activity.setSeckillTime(seckillTime);
        //查询当前活动对应的信息
        activity.setBegintime(TimeUtil.mergeDate(activity.getStartdate(),activity.getSeckillTime().getStarttime()));
        activity.setEndtime(TimeUtil.mergeDate(activity.getStartdate(),activity.getSeckillTime().getEndtime()));

        //如果结束时间<开始时间，则重新计算结束时间
        if(activity.getEndtime().getTime()<=activity.getBegintime().getTime()){
            activity.setEndtime(TimeUtil.mergeDate(TimeUtil.addDateHour(activity.getStartdate(),24),activity.getSeckillTime().getEndtime()));
        }
        activity.setTimeId(activity.getSeckillTime().getId());
        float times = TimeUtil.dif2hour(activity.getBegintime(), activity.getEndtime());
        activity.setTotalTime(times);
        //添加
        activityMapper.insert(activity);
    }

    /**
     * 根据ID查询Activity
     * @param id
     * @return
     */
    @Override
    public Activity findById(String id){
        Activity activity =  activityMapper.selectById(id);
        //查询出所有活动时间
        SeckillTime seckillTime = seckillTimeMapper.selectById(activity.getTimeId());
        //查询出当前
        Map<String,Object> mp = new HashMap<String,Object>();
        mp.put("status",1);
        List<SeckillTime> seckillTimes = seckillTimeMapper.selectByMap(mp);
        activity.setSeckillTime(seckillTime);
        return activity;
    }

    /**
     * 查询Activity全部数据
     * @return
     */
    @Override
    public List<Activity> findAll() {
        return activityMapper.selectList(null);
    }

    /***
     * 活动上线下线
     * @param id
     * @param isup
     */
    @Override
    public void isUp(String id, int isup) {
        Activity activity = new Activity();
        activity.setId(id);
        activity.setStatus(isup);
        activityMapper.updateById(activity);
    }

    @Override
    public List<Activity> times() {
        return activityMapper.times();
    }

    /***
     * 创建一个活动
     * @param seckillTime
     */
    @Override
    public Activity createActivity(SeckillTime seckillTime) {
        Activity activity = new Activity();
        activity.setStartdate(TimeUtil.getTimes(0));
        activity.setName(seckillTime.getName());
        activity.setStatus(1);
        //开始时间、结束时间
        //activity.setBegintime(TimeUtil.replaceDate1(TimeUtil.getTimes(0),seckillTime.getStarttime()));
        //activity.setEndtime(TimeUtil.replaceDate1(TimeUtil.getTimes(0),seckillTime.getEndtime()));

        //如果结束时间<开始时间，则重新计算结束时间
        if(activity.getEndtime().getTime()<=activity.getBegintime().getTime()){
            //activity.setEndtime(TimeUtil.replaceDate1(TimeUtil.addDateHour(TimeUtil.getTimes(0),24),seckillTime.getEndtime()));
        }
        activity.setTimeId(seckillTime.getId());
        float times = TimeUtil.dif2hour(activity.getBegintime(), activity.getEndtime());
        activity.setTotalTime(times);
        activityMapper.insert(activity);
        return activity;
    }

    /***
     * 删除所有活动
     */
    @Override
    public void deleteAll() {
        activityMapper.delete(null);
    }
}
